<?PHP
$title = "csweb - Asset Manager";
require_once('../includes/config.php');
require_once('includes/dbfunctions.php');



?>

<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/container/assets/container-core.css">
<link rel="stylesheet" href="http://yui.yahooapis.com/2.7.0/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css">
<!--    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/container/assets/skins/sam/container.css"> -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/treeview/assets/skins/sam/treeview.css" />
<link rel="stylesheet" type="text/css" href="includes/folders.css" />
<link rel="stylesheet" type="text/css" href="includes/styles.css" />

<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/dragdrop/dragdrop-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/connection/connection-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/treeview/treeview-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/container/container-min.js"></script>
<script type="text/javascript" src="../jquery-1.3.2.js"></script>


<style tyle="text/css">

    h1{
        font-size: 1.45em;
        font-weight: bold;
    }


    #custom-doc { width: 100%; min-width: 250px; }


    #example {height:20em;}

    /* Aqua Panel Skin CSS */

    /* Provide skin for default Panel elements, ontop of container-core.css */
    #editPanel_c.yui-panel-container.shadow .underlay,
    #myPanel_c.yui-panel-container.shadow .underlay {
        position:absolute;
        left:3px;
        right:-3px;
        top:3px;
        bottom:-3px;
        background-color:#000;
        opacity:0.12;
        filter:alpha(opacity=12);
    }

    #editPanel.yui-panel,
    #myPanel.yui-panel {
        border:none;
        overflow:visible;
        background-color:transparent;
    }
    #editPanel.yui-panel .container-close,
    #myPanel.yui-panel .container-close {
        position:absolute;
        top:3px;
        left:4px;
        height:18px;
        width:17px;
        background:url(img/aqua-hd-close.gif) no-repeat;
    }

    /* span:hover not supported on IE6 */
    #editPanel.yui-panel .container-close:hover,
    #myPanel.yui-panel .container-close:hover {
        background:url(img/aqua-hd-close-over.gif) no-repeat;
    }
    #editPanel.yui-panel .hd,
    #myPanel.yui-panel .hd {
        padding:0;
        border:none;
        background:url(img/aqua-hd-bg.gif) repeat-x;
        color:#000;
        height:22px;
        margin-left:6px;
        margin-right:6px;
        text-align:center;
        overflow:visible;
    }
    #editPanel.yui-panel .bd,
    #myPanel.yui-panel .bd {
        overflow:hidden;
        padding:4px;
        border:1px solid #aeaeae;
        background-color:#FFF;
    }
    #editPanel.yui-panel .ft,
    #myPanel.yui-panel .ft {
        font-size:75%;
        color:#666;
        padding:2px;
        overflow:hidden;
        border:1px solid #aeaeae;
        border-top:none;
        background-color:#dfdfdf;
    }

    /* Provide skin for custom elements */
    #editPanel.yui-panel .hd span,
    #myPanel.yui-panel .hd span {
        vertical-align:middle;
        line-height:22px;
        font-weight:bold;
    }
    #editPanel.yui-panel .hd .tl,
    #myPanel.yui-panel .hd .tl {
        width:7px;
        height:22px;
        top:0;
        left:0px;
        background:url(img/aqua-hd-lt.gif) no-repeat;
        position:absolute;
    }

    #editPanel.yui-panel .hd .tr,
    #myPanel.yui-panel .hd .tr {
        width:7px;
        height:22px;
        top:0;
        right:0px;
        background:url(img/aqua-hd-rt.gif) no-repeat;
        position:absolute;
    }

    #editPanel.yui-panel label{
        width: 200px;
        text-align: right;
        display: block;
    }

</style>



<script type="text/javascript">


    //global variable to allow console inspection of tree:
    var tree;
    var currentNode;

    function refreshFiles(){



        $("#browser").load("am_tree.php");



    }

    $(document).ready(function() {

        myPanel = new YAHOO.widget.Panel("myPanel",{
            width: "400px",
            fixedcenter: true,
            constrainviewport: true,
            close: true,
            draggable: true,
            underlay: "shadow",
            modal: true,
            visible: false
        });

        editPanel = new YAHOO.widget.Panel("editPanel",{
            width: "400px",
            fixedcenter: true,
            constrainviewport: true,
            close: true,
            draggable: true,
            underlay: "shadow",
            modal: true,
            visible: false
        });

        myPanel.beforeHideEvent.subscribe(function(e,a,o){
            // TODO: check input
        });

        myPanel.render();
        editPanel.render();

        var onUploadButtonClick = function(e){
            YAHOO.util.Connect.setForm('uploadForm', true);  // second arg, is file upload form

            var uploadHandler = {
                upload: function(o) {
                    // alert(o.responseText);
                    myPanel.hide()
                    refreshFiles();
                }
            };

            YAHOO.util.Connect.asyncRequest('POST', 'am_upload.php', uploadHandler);
        };


        var onEditPanelDoneButtonClick = function(e){
            // YAHOO.util.Connect.setForm('uploadForm', true);  // second arg, is file upload form

             var editCallback =
            {
              success: function(o) {
//                  alert(o.responseText);
                  window.location.reload();
//                    editPanel.hide();

              },
              failure: function(o) {
                  editPanel.hide();
              }
            }

            $("#editID").val(currentNode.data.myNodeId);
            YAHOO.util.Connect.setForm('editForm',false);
            var cObj = YAHOO.util.Connect.asyncRequest('POST', 'am_edit.php', editCallback);
        };



        var onCancelButtonClick = function(e){
            $("#uploadTitle").val("");
            $("#uploadDescription").val("");
            $("#uploadFile").val("");
            $("#uploadPath").val("");
            myPanel.hide();
        };

        var onEditPanelCancelButtonClick = function(e){
            editPanel.hide();
        };

        YAHOO.util.Event.on('uploadButton', 'click', onUploadButtonClick);
        YAHOO.util.Event.on('cancelButton','click',onCancelButtonClick);
        YAHOO.util.Event.on('editPanelDoneButton', 'click', onEditPanelDoneButtonClick);
        YAHOO.util.Event.on('editPanelCancelButton','click',onEditPanelCancelButtonClick);


});

    function deleteCurrent(){
        if(currentNode == null){
            // don't do anything'

        } else {
            // delete currently selected asset
            $("#stuff_message").load("am_delete.php?fileID="+currentNode.data.myNodeId);
            refreshFiles();
            $("#fileviewer").text("");
        }

    }

    function editCurrent(){

        if(currentNode == null){
            // don't do anything'
            alert("currentNode == null");

        } else {



        $.ajax({
           type: "GET",
           url: "am_retrieve.php",
           data: "fileID="+currentNode.data.myNodeId,
           dataType: "script",
           success: function(msg){
             $("#editTitle").attr("value",editTitle);
             $("#editDescription").attr("value",editDescription);
             $("#editPath").attr("value",editPath);
             editPanel.show();
           },
           
           error: function(msg){
               alert("Error retrieving data.\nPlease try again.");
           }

         });

        }





    }

    function panelDone(){

    }

    function panelCancel(){

    }

    var lastFocus;

    function mainTreeInit(){

        tree = new YAHOO.widget.TreeView("treeDiv1");

        refreshFiles();

        tree.subscribe("focusChanged", function(oargs){

            if(oargs.newNode.hasChildren()){
                $("#stuff_message").text("isn't a picture\n");
                $("#fileviewer").text("");
                currentNode = null;
            } else {
                $("#stuff_message").text(
                oargs.newNode.label.substring(
                oargs.newNode.label.indexOf(">")+1,
                oargs.newNode.label.lastIndexOf("<")));
                $("#fileviewer").load("am_display.php?fileID="+oargs.newNode.data.myNodeId);
                currentNode = oargs.newNode;

            }
        });
    }

    YAHOO.util.Event.onDOMReady(mainTreeInit);

</script>

</head>


<body>
    <div id="doc3" class="yui-t7">
        <div id="hd">
            <h1 style="font-size: 1.45em; font-weight: bold;">csweb - Asset Manager</h1>
            &nbsp;<br>

            <button onclick='myPanel.show()'>Upload</button>
            <button onclick='deleteCurrent()'>Delete</button>
            <button onclick='editCurrent()'>Edit</button>
        </div>
        <div id="bd">
            <div class="yui-gd">
                <div class="yui-u first">
                    First
                    <div id="browser"></div>
                    <!--                    <div id="stuff_message"></div> -->
                    <div id="treeDiv1"> </div>

                </div>
                <div class="yui-u">
                    Second
                    <div id="fileviewer"><span /></div>
                </div>

            </div>
        </div>
        <div id="ft"></div>
    </div>

    <div id="myPanel">
        <div class="hd"><div class="tl"></div><span>File Upload</span><div class="tr"></div></div>
        <div class="bd">
            Please select a file to upload:
            <form action="am_upload.php" enctype="multipart/form-data" method="post" id="uploadForm">
                <input type="file" name="file" id="uploadFile" /><br>
                <label>Title:</label><input type="text" name="Title" id="uploadTitle" /><br/>
                <label>Descripiton:</label><input type="text" name="Description" id="uploadDescription" /><br/>
                <label>Path:</label><input type="text" name="Path" id="uploadPath"><br/>
                <input type="button" id="cancelButton" value="Cancel"/>
                <input type="button" id="uploadButton" value="Done"/>
            </form>
        </div>
        <div class="ft">&nbsp;</div>
    </div>

    <div id="editPanel">
        <div class="hd"><div class="tl"></div><span>Edit Info</span><div class="tr"></div></div>
        <div class="bd">
            <span id="editPanelForm">
            <form action="am_edit" enctype="multipart/form-data" method="post" id="editForm" >
                <input type="hidden" name="ID" id="editID" />
                <label>Title: </label><input type="text" name="Title" id="editTitle"><br/>
                <label>Description: </label><input type="text" name="Description" id="editDescription"><br/>
                <label>Path: </label><input type="text" name="Path" id="editPath"><br/>
                <input type="button" id="editPanelCancelButton" value="Cancel"/>
                <input type="button" id="editPanelDoneButton" value="Done"/>
            </form>
            </span>
        </div>
        <div class="ft">&nbsp;</div>

    </div>



</body>
</html>